import Vue from 'vue'
import VueRouter from 'vue-router'
const Home = () => import('@/components/home/Home')
const User = () => import('@/components/user/User')
const UserInfo = () => import('@/components/user/child/UserInfo')
const Avatar = () => import('@/components/user/child/Avatar')
const Error = () => import('@/components/error/Error')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home
  },
  {
    path: '/user/:id',
    component: User,
    children: [
      {
        path: 'profile',
        component: UserInfo
      },
      {
        path: 'avatar',
        component: Avatar
      }
    ]
  },
  {
    path: '/notfound',
    component: Error
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const arr = ['tom', 'rose']
  const arr1 = ['profile', 'avatar']
  if (to.path.indexOf('/user') !== -1) {
    const strArr = to.path.split('/')
    const isTure = strArr[strArr.indexOf('user') + 2]
    if (!arr.includes(strArr[strArr.indexOf('user') + 1]) || !arr1.includes((isTure || 'profile'))) {
      return next('/notfound')
    }
  }
  next()
})

export default router
